package algorithm.niuke;

/**
 * 数的N次方的问题
 *
 * @author jack.wu
 * @version 1.0
 * @date 2020-05-11
 */
public class PowerNumber {

    public static void main(String[] args) {

        double val = power(2, -10);
        System.out.println(val);
    }

    /**
     * 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
     *
     * @param base     底数
     * @param exponent 指数
     * @return 结果
     */
    public static double power(int base, int exponent) {
        if (base == 0 && exponent == 0) {
            return 0;
        }

        double result = 1D;

        if (exponent > 0) {
            for (int i = 0; i < exponent; i++) {
                result *= base;
            }
        } else if (exponent < 0) {
            exponent = -exponent;
            for (int j = 0; j < exponent; j++) {
                result *= base;
            }
            result = 1 / result;
        }

        return result;
    }
}
